package com.zhss.eshop.schedule.service.Impl;

import com.zhss.eshop.common.util.DateProvider;
import com.zhss.eshop.common.util.ObjectUtils;
import com.zhss.eshop.order.domain.dto.OrderInfoDTO;
import com.zhss.eshop.order.domain.dto.OrderItemDTO;
import com.zhss.eshop.schedule.constant.StockUpdateEvent;
import com.zhss.eshop.schedule.domain.SaleDeliveryScheduleResult;
import com.zhss.eshop.schedule.domain.model.ScheduleOrderPickingItem;
import com.zhss.eshop.schedule.domain.model.ScheduleOrderSendOutDetail;
import com.zhss.eshop.schedule.mapper.ScheduleOrderPickingItemMapper;
import com.zhss.eshop.schedule.mapper.ScheduleOrderSendOutDetailMapper;
import com.zhss.eshop.schedule.service.SaleDeliveryOrderBuilder;
import com.zhss.eshop.schedule.service.SaleDeliveryScheduler;
import com.zhss.eshop.schedule.service.ScheduleService;
import com.zhss.eshop.schedule.stock.ScheduleStockUpdater;
import com.zhss.eshop.schedule.stock.ScheduleStockUpdaterFactory;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderDTO;
import com.zhss.eshop.wms.domain.dto.WmsReturnGoodsInputOrderDTO;
import com.zhss.eshop.wms.domain.dto.WmsSaleDeliveryOrderDTO;
import com.zhss.eshop.wms.service.WmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 调度中心对外接口service组件
 *
 */
@Service
public class ScheduleServiceImpl implements ScheduleService {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleServiceImpl.class);
    /**
     * 销售出库单构建器工厂
     */
    @Autowired
    private SaleDeliveryOrderBuilderFactory saleDeliveryOrderBuilderFactory;
    /**
     * wms中心对外接口service组件
     */
    @Autowired
    private WmsService wmsService;
    /**
     * 销售出库调度器
     */
    @Autowired
    private SaleDeliveryScheduler saleDeliveryScheduler;
    /**
     * 拣货条目管理DAO组件
     */
    @Autowired
    private ScheduleOrderPickingItemMapper pickingItemDAO;
    /**
     * 发货明细管理DAO组件
     */
    @Autowired
    private ScheduleOrderSendOutDetailMapper sendOutDetailDAO;
    /**
     * 日期辅助组件
     */
    @Autowired
    private DateProvider dateProvider;
    /**
     * 库存更新组件工厂
     */
    @Autowired
    private ScheduleStockUpdaterFactory stockUpdaterFactory;

    @Override
    public Boolean informPurchaseInputFinished(WmsPurchaseInputOrderDTO purchaseInputOrderDTO) {
        return null;
    }
    /**
     * 通知库存中心，“提交订单”事件发生了`
     * @param order 订单DTO
     * @return 处理结果
     */
    @Override
    public Boolean informSubmitOrderEvent(OrderInfoDTO order) {
        try {
            for(OrderItemDTO orderItem : order.getOrderItems()) {
                SaleDeliveryScheduleResult scheduleResult =
                        saleDeliveryScheduler.schedule(orderItem);

                List<ScheduleOrderPickingItem> pickingItems = ObjectUtils.convertList(
                        scheduleResult.getPickingItems(), ScheduleOrderPickingItem.class);
                List<ScheduleOrderSendOutDetail> sendOutDetails = ObjectUtils.convertList(
                        scheduleResult.getSendOutDetails(), ScheduleOrderSendOutDetail.class);

                //将订单调度条目落地
                for(ScheduleOrderPickingItem pickingItem : pickingItems) {
                    pickingItem.setOrderInfoId(orderItem.getOrderInfoId());
                    pickingItem.setOrderItemId(orderItem.getId());
                    pickingItem.setGmtCreate(dateProvider.getCurrentTime());
                    pickingItem.setGmtModified(dateProvider.getCurrentTime());
                    pickingItemDAO.insertSelective(pickingItem);
                }
                for(ScheduleOrderSendOutDetail sendOutDetail : sendOutDetails) {
                    sendOutDetail.setOrderInfoId(orderItem.getOrderInfoId());
                    sendOutDetail.setOrderItemId(orderItem.getId());
                    sendOutDetail.setGmtCreate(dateProvider.getCurrentTime());
                    sendOutDetail.setGmtModified(dateProvider.getCurrentTime());
                    sendOutDetailDAO.insertSelective(sendOutDetail);
                }
//                pickingItemDAO.batchSave(orderItem.getOrderInfoId(), orderItem.getId(), pickingItems);
//                sendOutDetailDAO.batchSave(orderItem.getOrderInfoId(), orderItem.getId(), sendOutDetails);

                ScheduleStockUpdater stockUpdater = stockUpdaterFactory.create(StockUpdateEvent.SUBMIT_ORDER, scheduleResult);
                stockUpdater.update();

                wmsService.informSubmitOrderEvent(scheduleResult);
            }

            return true;
        } catch (Exception e) {
            logger.error("error", e);
            return false;
        }
    }

    @Override
    public Boolean informPayOrderEvent(OrderInfoDTO orderDTO) {
        return null;
    }

    @Override
    public Boolean informCancelOrderEvent(OrderInfoDTO orderDTO) {
        return null;
    }

    @Override
    public Boolean informReturnGoodsInputFinished(WmsReturnGoodsInputOrderDTO returnGoodsInputOrderDTO) {
        return null;
    }

    /**
     * 调度销售出库
     * @param order 订单DTO
     * @return 处理结果
     */
    @Override
    public Boolean scheduleSaleDelivery(OrderInfoDTO order) {
        try {
            SaleDeliveryOrderBuilder saleDeliveryOrderBuilder =
                    saleDeliveryOrderBuilderFactory.get();

            WmsSaleDeliveryOrderDTO saleDeliveryOrder = saleDeliveryOrderBuilder
                    .setOrderRelatedData(order)
                    .createSaleDeliveryOrderItems(order.getOrderItems())
                    .createSendOutOrder(order)
                    .createLogisticOrder(order)
                    .initStatus()
                    .initTimes()
                    .create();

            wmsService.createSaleDeliveryOrder(saleDeliveryOrder);

            return true;
        } catch (Exception e) {
            logger.error("error", e);
            return false;
        }
    }

    @Override
    public SaleDeliveryScheduleResult getScheduleResult(Long orderInfoId, Long goodsSkuId) {
        return null;
    }
}
